package com.hsnn.medstgmini.pro.base.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.ViewDrug;
import com.hsnn.medstgmini.pro.base.model.BaseBakpurchase;
import com.hsnn.medstgmini.pro.base.model.ViewBakpurchase;
import com.hsnn.medstgmini.pro.base.service.BaseBakpurchaseManager;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/baseBakpurchase")
public class BaseBakpurchaseController {
	
	private static final Logger log = Logger.getLogger(BaseBakpurchaseController.class);
	
	@Autowired
	private BaseBakpurchaseManager baseBakpurchaseManager;

	@Autowired
	private DicAreaManager dicAreaManager;
	
    @Value("#{sysProp['system.province']}")
    private String sysProvince;
    
	/**
	 * @category 跳转到备案页面
	 * @author 韩守松
	 * @date 2015年6月19日
	 * @param @param request
	 * @param @return
	 */
	@RequestMapping(value = "/toDataJsp", method = { RequestMethod.GET, RequestMethod.POST })
	public String toDataJsp(HttpServletRequest request) {
		return "/pro/bak/bakDrugSearchYi";
	}

	/**
	 * 备案审批查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/getDataList")
	@ResponseBody
	public Pagination getDataList(HttpServletRequest request, ViewBakpurchase viewBakpurchase, Pagination page, ViewDrug viewDrug) {
		List<ViewBakpurchase> viewBakpurchaseList =  null;
		page = new Pagination(request);

		try {
            String drugCode = request.getParameter("drugCode");
            String drugName = request.getParameter("drugName");
			String compName = request.getParameter("compName");
			String hospName = request.getParameter("hospName");
			String auditStatus = request.getParameter("auditStatus");
			String appStartDate = request.getParameter("appStartDate");
			String appEndDate = request.getParameter("appEndDate");
			String sysAreaId = request.getParameter("sysAreaId");
			String area1 = request.getParameter("area1");
			String area2 = request.getParameter("area2");
			String auditStartDate = request.getParameter("auditStartDate");
			String auditEndDate = request.getParameter("auditEndDate");

			String areaId = sysProvince;
			if (StringUtils.isNotBlank(area2)) {
			    areaId = area2;	
			} else if (StringUtils.isNotBlank(area1)) {
				areaId = area1.replaceAll("0*$", "");
			} else {
				areaId = sysProvince.replaceAll("0*$", "");
			}
			
			page.getConditions().put("drugCode", drugCode);
			page.getConditions().put("drugName", drugName);
			page.getConditions().put("compName", compName);
			page.getConditions().put("hospName", hospName);
			page.getConditions().put("auditStatus", auditStatus);
			page.getConditions().put("appStartDate", appStartDate);
			page.getConditions().put("appEndDate", appEndDate);
			page.getConditions().put("areaId", areaId);
			page.getConditions().put("auditStartDate", auditStartDate);
			page.getConditions().put("auditEndDate", auditEndDate);

			viewBakpurchaseList = baseBakpurchaseManager.getBaseBakpurchaseList(page, viewBakpurchase);
			request.setAttribute("list", viewBakpurchaseList);
			
			if (log.isDebugEnabled()) {
			    log.debug("备案查询结果数-->" + viewBakpurchaseList.size());
			    log.debug("备案查询结果集-->" + viewBakpurchaseList);
			}
			
			page.setRecords(viewBakpurchase.getCount());
			page.setTotal(new Long(viewBakpurchase.getCount()%page.getMaxResults()==0?viewBakpurchase.getCount()/page.getMaxResults():viewBakpurchase.getCount()/page.getMaxResults()+1).intValue());
			page.setRows(viewBakpurchaseList);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}

	/**
	 * @category 跳转到审核页面
	 * @author 韩守松
	 * @date 2015年6月19日
	 * @param @param request
	 * @param @return
	 */
	@RequestMapping(value = "/toDataFormJsp", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String toDataFormJsp(HttpServletRequest request,
			ViewBakpurchase viewBakpurchase, Pagination page) {
		List<ViewBakpurchase> viewBakpurchaseList = new ArrayList<ViewBakpurchase>();
		Timestamp date1 = null;
		Timestamp date2 = null;
		String bakPurId = request.getParameter("bakPurId");
		if (StringUtils.isNotEmpty(bakPurId)) {
			viewBakpurchase.setBakPurId(bakPurId);
		}
		viewBakpurchaseList = baseBakpurchaseManager.getBaseBakpurchaseList(
				page, viewBakpurchase, date1, date2);
		if (viewBakpurchaseList.size() > 0) {
			request.setAttribute("list", viewBakpurchaseList.get(0));
			return "/pro/bak/bakDrugSearchWeiForm";
		} else {
			return "redirect:/baseBakpurchase/toDataJsp.html";
		}

	}
	
	@RequestMapping(value="/toDataDetaulJsp",method={RequestMethod.GET,RequestMethod.POST})
	public String toDataDetailJsp(HttpServletRequest request,
			ViewBakpurchase viewBakpurchase, Pagination page){
		List<ViewBakpurchase> viewBakpurchaseList = new ArrayList<ViewBakpurchase>();
		Timestamp date1 = null;
		Timestamp date2 = null;
		String bakPurId = request.getParameter("bakPurId");
		if (StringUtils.isNotEmpty(bakPurId)) {
			viewBakpurchase.setBakPurId(bakPurId);
		}
		viewBakpurchaseList = baseBakpurchaseManager.getBaseBakpurchaseList(
				page, viewBakpurchase, date1, date2);
		if (viewBakpurchaseList.size() > 0) {
			request.setAttribute("list", viewBakpurchaseList.get(0));
		return "/pro/bak/bakDrugSearchDetail";
		} else {
			return "redirect:/baseBakpurchase/toDataJsp.html";
		}
	}

	@RequestMapping(value = "/dataAddAndEdit", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public boolean dataAddAndEdit(HttpServletRequest request, BaseBakpurchase baseBakpurchase) {
		try {
			String bakPurId = request.getParameter("bakPurId");
			String auditStatus = request.getParameter("auditStatus");
			String auditRemaks = request.getParameter("auditRemaks");
			if (StringUtils.isNotEmpty(bakPurId)) {
				baseBakpurchase.setBakPurId(bakPurId);
			}
			if (StringUtils.isNotEmpty(auditStatus)) {
				baseBakpurchase.setAuditStatus(Short.valueOf(auditStatus));
			}
			baseBakpurchase.setAuditRemaks(auditRemaks);
			boolean flag = baseBakpurchaseManager
					.dataAddAndEdit(baseBakpurchase);
			if (flag) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			log.error("数据请求错误-->", e);
			return false;
		}

	}
}